package bean.Interceptor;

import bean.BeanFactory;

import javax.annotation.Resource;
import java.lang.reflect.Field;

public class FieldInterceptor implements Interceptor{

    @Override
    public Object filter(Object bean, BeanFactory factory) throws IllegalAccessException {

        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Resource annotation = field.getAnnotation(Resource.class);
            if(annotation == null){
                continue;
            }
            if ( !annotation.name().isEmpty()){

                String name = annotation.name();
                Object a = factory.getBean(name);

                field.set(bean,a);
            }else {
                Object a = factory.getBean(field.getType());
                field.set(bean,a);
            }
        }

        return bean;
    }
}
